package com.salon.client.widgets;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.MasterBean;
import com.salon.client.tabItems.MasterTabItemPanel;

public class MasterDialog extends Dialog {
	protected TextField<String> name = new TextField<String>();
	protected TextField<String> surname = new TextField<String>();
	protected TextField<String> lastname = new TextField<String>();
	protected TextField<String> address = new TextField<String>();
	protected TextField<String> phone = new TextField<String>();
	protected NumberField salary = new NumberField();
	protected NumberField percent = new NumberField();
	protected DateField dob = new DateField();
	protected MasterBean bean = null;
	private MasterTabItemPanel parentPanel;
	
	
	public MasterDialog(MasterTabItemPanel panel, MasterBean bean) {
		parentPanel = panel;
		this.bean = bean;
		setHeading("Добавление мастера");
		FormLayout layout = new FormLayout();
	    layout.setLabelWidth(130);
	    layout.setDefaultWidth(200);
	    setLayout(layout);
	    
	    setButtonAlign(HorizontalAlignment.CENTER);
	    setButtons(Dialog.OKCANCEL);
	    
	    setIcon(IconHelper.createStyle("user"));
	    setModal(true);
	    setBodyBorder(true);
	    setBodyStyle("padding: 8px;background: none");
	    setWidth(400);
	    setResizable(false);
	    
	    surname.setFieldLabel("Фамилия");
	    add(surname);
	    name.setFieldLabel("Имя");
	    add(name);
	    lastname.setFieldLabel("Отчество");
	    add(lastname);
	    dob.setFieldLabel("Дата рождения");
	    add(dob);
	    address.setFieldLabel("Адрес");
	    add(address);
	    phone.setFieldLabel("Телефон");
	    add(phone);
	    salary.setFieldLabel("Оклад");
	    add(salary);
	    percent.setFieldLabel("Проценты");
	    add(percent);
	    
	    if (bean != null) {
	    	fillData();
	    }
	    
	    Button ok = getButtonById(Dialog.OK);
		ok.setText("ОК");
		ok.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				if (!isValidate()) {
					return;
				}
				if(MasterDialog.this.bean == null) {
					MasterDialog.this.bean = new MasterBean();
					if (!getData()) {
						return;
					}
					((GreetingServiceAsync) Registry.get(Salon.SERVICE)).
					addMaster(MasterDialog.this.bean, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Info.display("Не удалось", "Добавить работника");
							
						}

						@Override
						public void onSuccess(Void result) {
							Info.display("Добавление работника", "Успешно");
							MasterDialog.this.hide();
							MasterDialog.this.parentPanel.refreshStore();
						}
						
					});
				}
				else {
					if (!getData()) {
						return;
					}
					((GreetingServiceAsync) Registry.get(Salon.SERVICE)).
					updateMaster(MasterDialog.this.bean, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Info.display("Не удалось", "Редактировать работника");
							
						}

						@Override
						public void onSuccess(Void result) {
							Info.display("Редактирование работника", "Успешно");
							MasterDialog.this.hide();
							MasterDialog.this.parentPanel.refreshStore();
						}
						
					});
				}
				
				
			}
			
		});
		
	    Button cancel = getButtonById(Dialog.CANCEL);
	    cancel.setText("Отмена");
	    cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				MasterDialog.this.hide();
				
			}
	    });
	}
	private boolean isValidate() {
		if (surname.getValue() == null || surname.getValue().isEmpty()) {
			Info.display("Введите", "Фамилию мастера");
			surname.focus();
			return false;
		}
		if (salary.getValue() == null) {
			Info.display("Введите Оклад", "Работника");
			salary.focus();
			return false;
		}
		
		if ((percent.getValue() == null) || (percent.getValue().intValue() < 0 || percent.getValue().intValue() > 100)) {
			Info.display("Введите Проценты", "в диапазоне от 0 до 100");
			percent.focus();
			return false;
		}
		return true;
		
		
	}
	private boolean getData() {
		//if (surname.getValue() != null && !surname.getValue().isEmpty()) {
			bean.setSurname(surname.getValue());
		//}
		//if (name.getValue() != null && !name.getValue().isEmpty()) {
			bean.setName(name.getValue());
		//}
		//if (lastname.getValue() != null && !lastname.getValue().isEmpty()) {
			bean.setLastName(lastname.getValue());
		//}
		//if (phone.getValue() != null && !phone.getValue().isEmpty()) {
			bean.setPhone(phone.getValue());
		//}
		//if (address.getValue() != null && !address.getValue().isEmpty()) {
			bean.setAddress(address.getValue());
		//}
		
		//if (salary.getValue() != null) {
			bean.setSalary(salary.getValue().intValue());
		//}
		
		/*if (percent.getValue() != null) {
			if (percent.getValue().intValue() < 0 || percent.getValue().intValue() > 100) {
				Info.display("Введите Проценты", "в диапазоне от 0 до 100");
				percent.focus();
				return false;
			}*/
			bean.setPercent(percent.getValue().intValue());
		//}
		//if (dob.getValue() != null) {
			bean.setDob(dob.getValue());
		//}
		return true;
	}
	
	private void fillData() {
		if (bean.getSurname() != null) {
			surname.setValue(bean.getSurname());
		}
		if (bean.getName() != null) {
			name.setValue(bean.getName());
		}
		if (bean.getLastName() != null) {
			lastname.setValue(bean.getLastName());
		}
		if (bean.getAddress() != null) {
			address.setValue(bean.getAddress());
		}
		if (bean.getPhone() != null) {
			phone.setValue(bean.getPhone());
		}
		if (bean.getDob() != null) {
			dob.setValue(bean.getDob());
		}
		
		salary.setValue(bean.getSalary());
		percent.setValue(bean.getPercent());
	}
}